package rentadomus

import org.springframework.dao.DataIntegrityViolationException

class ContractController {



    def list(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        def contracts=[]
        def myContracts=[]
        def places=Place.findAllByIdPerson(session.person)
        for(Contract contract:Contract.list()){
            if(contract.person.id==session.person.id){
                contracts.add contract
            }else{
                for(Place place:places){
                    if(contract.place==place){
                        myContracts.add contract
                    }
                }
            }
        }
//        [contractInstanceList: Contract.list(params), contractInstanceTotal: Contract.count(),contracts:contracts,contractsTotal:contracts.size()]
        [contractInstanceList: myContracts, contractInstanceTotal: myContracts.size(),contracts:contracts,contractsTotal:contracts.size()]
    }

    def create() {
        def places=Place.findAllByIdPerson(session.person)
        def myPlaces=[]
        places.each(){
            def exist=false
            for(Contract contract:Contract.list()){
                if(contract.place==it){
                    exist=true; 
                    break;
                }
            }
            if (!exist) {
                def local=Local.findByIdPlace(it)
                def office=Office.findByIdPlace(it)
                def wareHouse=Warehouse.findByIdPlace(it)
                def dwelling=Dwelling.findByIdPlace(it)
                if(local){
                    myPlaces.add it
                }else if(office){
                    myPlaces.add it
                }else if(wareHouse){
                    myPlaces.add it
                }else if(dwelling){
                    def house=House.findByIdDwelling(dwelling)
                    def apartament=Apartament.findByIdDwelling(dwelling)
                    if(house){
                        myPlaces.add it
                    }else if(apartament){
                        myPlaces.add it
                    }
                }
            }
        }
        [contractInstance: new Contract(params), places:myPlaces]
    }

    def save() {
//        if (!Person.findByIdentification(params.person.id)) {
        def person=Person.findByIdentification(params.person.identification)
        if (!person) {
//            flash.message = message(code: 'contract.not.created.message', args: [params.person.id])
            flash.message = message(code: 'contract.not.created.message', args: [params.person.identification])
            redirect(action: "create")
        }else{
            def contractInstance = new Contract(params)
            contractInstance.person=person
            contractInstance.save(flush: true)
            flash.message = message(code: 'default.created.message', args: [message(code: 'contract.label', default: 'Contract'), contractInstance.id])
            redirect(action: "list", id: contractInstance.id)
        }
    }

    def show(Long id) {
        def isMyContract="block"
        def contractInstance = Contract.get(id)
        if (!contractInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'contract.label', default: 'Contract'), id])
            redirect(action: "list")
            return
        }else{
            if(contractInstance.person.id==session.person.id){
                isMyContract="none"
            }
        }
        [contractInstance: contractInstance,isMyContract:isMyContract]
    }
    def edit(Long id) {
        def contractInstance = Contract.get(id)
        if (!contractInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'contract.label', default: 'Contract'), id])
            redirect(action: "list")
            return
        }
        def places=Place.findAllByIdPerson(session.person)
        def myPlaces=[]
        places.each(){
            def exist=false
            for(Contract contract:Contract.list()){
                if(contract.place==it){
                    exist=true; 
                    break;
                }
            }
            if (!exist) {
                def local=Local.findByIdPlace(it)
                def office=Office.findByIdPlace(it)
                def wareHouse=Warehouse.findByIdPlace(it)
                def dwelling=Dwelling.findByIdPlace(it)
                if(local){
                    myPlaces.add it
                }else if(office){
                    myPlaces.add it
                }else if(wareHouse){
                    myPlaces.add it
                }else if(dwelling){
                    def house=House.findByIdDwelling(dwelling)
                    def apartament=Apartament.findByIdDwelling(dwelling)
                    if(house){
                        myPlaces.add it
                    }else if(apartament){
                        myPlaces.add it
                    }
                }
            }
        }
        [contractInstance: contractInstance, places:myPlaces]
    }

    def update(Long id, Long version) {
        def contractInstance = Contract.get(id)
        if (!contractInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'contract.label', default: 'Contract'), id])
            redirect(action: "list")
            return
        }

        if (version != null) {
            if (contractInstance.version > version) {
                contractInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'contract.label', default: 'Contract')] as Object[],
                          "Another user has updated this Contract while you were editing")
                render(view: "edit", model: [contractInstance: contractInstance])
                return
            }
        }

        contractInstance.properties = params

        if (!contractInstance.save(flush: true)) {
            render(view: "edit", model: [contractInstance: contractInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'contract.label', default: 'Contract'), contractInstance.id])
        redirect(action: "show", id: contractInstance.id)
    }

//    def delete(Long id) {
//        def contractInstance = Contract.get(id)
//        if (!contractInstance) {
//            flash.message = message(code: 'default.not.found.message', args: [message(code: 'contract.label', default: 'Contract'), id])
//            redirect(action: "list")
//            return
//        }
//
//        try {
//            contractInstance.delete(flush: true)
//            flash.message = message(code: 'default.deleted.message', args: [message(code: 'contract.label', default: 'Contract'), id])
//            redirect(action: "list")
//        }
//        catch (DataIntegrityViolationException e) {
//            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'contract.label', default: 'Contract'), id])
//            redirect(action: "show", id: id)
//        }
//    }
}
